# -*- coding: utf-8 -*-
# @Author: fanmingxiang@touty.io
# @Date: 2023/3/18 14:54
import os
import random
import unittest

from htmltestreport import HTMLTestReport

from apitests.run_cases import TestAutoCase
from test_reports.models import TestReports, ReportDescribe


def run_suite(report_id, report_title):
    report_url = str(random.randint(1000000000, 9999999999)) + '.html'
    report_path = r'F:\Pycharm\workspace\ApiAutoTestPlatform\apitests\templates\report' + os.sep + report_url
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestAutoCase))
    # suite.addTest(unittest.TestLoader().loadTestsFromModule(run_cases))
    html_report = HTMLTestReport(report_path, report_title)
    report = html_report.run(suite)
    # test_report = TestReports.objects.filter(id=report_id).first()
    # test_report.test_all_count = report.testsRun
    # test_report.success_count = report.success_count
    # test_report.failure_count = report.failure_count
    # test_report.error_count = report.error_count
    # test_report.skip_count = report.skip_count
    # test_report.startTime = html_report.startTime
    # test_report.stopTime = html_report.stopTime
    # test_report.status = 1
    # test_report.report_url = report_url
    # test_report.save()
    # for result in report.result:
    #     report_describe = ReportDescribe(name=result[1], error_msg=result[3], status=result[0], test_report=test_report)
    #     report_describe.save()
    # print("结果详情", report.result)
    # print("结果状态", report.result[0][0],
    #       "用例名", report.result[0][1],
    #       "用例输入", report.result[0][2],
    #       "错误原因", report.result[0][3])
    # print("用例总数", report.testsRun)
    # print("用例成功数", report.success_count)
    # print("用例失败数", report.failure_count)
    # print("用例错误数", report.error_count)
    # print("用例跳过数", report.skip_count)
    # print("开始执行时间", html_report.startTime)
    # print("开始执行时间", html_report.stopTime)

    # 一期计划  先存入数据库  报告持续增加 从report取数据存入， 后期从unittest取数据存入一个入口即可

if __name__ == '__main__':
    test_report = TestReports.objects.last()
    run_suite(test_report.id,  test_report.title)